public static int minimumDifference1(int[] nums, int k) {
    if (k == 1) {
        return 0;
    }
    int ans = Integer.MAX_VALUE;
    Arrays.sort(nums);
    for (int i = 0; i < nums.length; i++) {
        for (int j = i + k - 1; j < nums.length; j++) {
            ans = Math.min(ans, nums[j] - nums[i]);
        }
    }
    return ans;
}

public static int minimumDifference(int[] nums, int k) {
    int ans = Integer.MAX_VALUE;
    Arrays.sort(nums);
    for (int i = 0; i + k - 1 < nums.length; i++) {
        ans = Math.min(ans, nums[i + k - 1] - nums[i]);
    }
    return ans;
}